package com.qf.Servlet;


import com.qf.Service.DoctorPointService;
import com.qf.Service.impl.DoctorPointServiceImpl;
import com.qf.Utils.JsonUtils;
import com.qf.Utils.ResultVo;
import com.qf.entity.DoctorPoint;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/doctorPointServlet")
public class DoctorPointServlet extends BaseServlet {
    private DoctorPointService doctorPointService = new DoctorPointServiceImpl();

    //    查
    public void selrctByPatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String patientName = req.getParameter("patientName");
        PrintWriter pw = resp.getWriter();
        ResultVo<List<DoctorPoint>> resultVo = new ResultVo<>();
        List<DoctorPoint> doctorPointDaos = doctorPointService.selectByPatientName(patientName);
        if (doctorPointDaos != null && doctorPointDaos.size() > 0) {
            resultVo.setData(doctorPointDaos);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(null);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    //    增
    public void addPatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String patientIdcard = req.getParameter("patient_idcard");
        String patientName = req.getParameter("patient_name");
        String doctorName = req.getParameter("doctor_name");
        String pointDate = req.getParameter("point_date");
        String pointPlace = req.getParameter("point_place");
        DoctorPoint doctorPoint = new DoctorPoint();
        doctorPoint.setId(Integer.parseInt(id));
        doctorPoint.setPatient_Idcard(Long.parseLong(patientIdcard));
        doctorPoint.setPatient_Name(patientName);
        doctorPoint.setDoctor_Name(doctorName);
        doctorPoint.setPoint_Date(pointDate);
        doctorPoint.setPoint_Place(pointPlace);
        int num = doctorPointService.addPatientName(doctorPoint);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    //    改
    public void updatePatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String patientIdcard = req.getParameter("patient_idcard");
        String patientName = req.getParameter("patient_name");
        String doctorName = req.getParameter("doctor_name");
        String pointDate = req.getParameter("point_date");
        String pointPlace = req.getParameter("point_place");
        DoctorPoint doctorPoint = new DoctorPoint();
        doctorPoint.setId(Integer.parseInt(id));
        doctorPoint.setPatient_Idcard(Long.parseLong(patientIdcard));
        doctorPoint.setPatient_Name(patientName);
        doctorPoint.setDoctor_Name(doctorName);
        doctorPoint.setPoint_Date(pointDate);
        doctorPoint.setPoint_Place(pointPlace);
        int num = doctorPointService.addPatientName(doctorPoint);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw, resultVo);
    }

    //删
    public void deleteByPatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String patientName, String patient_Idcard
        String patientName = req.getParameter("patientName");
        String patientIdcard = req.getParameter("patient_Idcard");
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        int num = doctorPointService.deletePatientName(patientName, patientIdcard);
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }
}
